package novel.front.novellist.test;

import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

/**
 * Created by sutong on 2018/7/1.
 */
public class GenerateExcelUtils {
    //歌曲评论Excel生成
    public static void generateCommentsExcel(MusicCommentMessage musicCommentMessage) throws IOException {

        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("歌曲评论");
        sheet.setDefaultColumnWidth(15);

        HSSFRow rowHead = sheet.createRow(0);

        HSSFCellStyle style = workbook.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);

        HSSFFont font = workbook.createFont();
        font.setColor(HSSFColor.LIGHT_BLUE.index);
        font.setFontHeightInPoints((short) 8);
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        style.setFont(font);

        HSSFCell cellHead = rowHead.createCell(0);
        cellHead.setCellValue("歌名");
        cellHead.setCellStyle(style);

        cellHead = rowHead.createCell(1);
        cellHead.setCellValue("评论类型");
        cellHead.setCellStyle(style);

        cellHead = rowHead.createCell(2);
        cellHead.setCellValue("评论用户昵称");
        cellHead.setCellStyle(style);

        cellHead = rowHead.createCell(3);
        cellHead.setCellValue("评论时间");
        cellHead.setCellStyle(style);

        cellHead = rowHead.createCell(4);
        cellHead.setCellValue("评论内容");
        cellHead.setCellStyle(style);

        cellHead = rowHead.createCell(5);
        cellHead.setCellValue("获赞数");
        cellHead.setCellStyle(style);

        HSSFCellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);

        List<MusicComment> comments = musicCommentMessage.getComments();

        for (int i = 0; i < comments.size(); i++) {
            MusicComment comment = comments.get(i);
            HSSFRow row = sheet.createRow(i + 1);

            HSSFCell cell = row.createCell(0);
            cell.setCellValue(musicCommentMessage.getSongTitle());
            cell.setCellStyle(cellStyle);

            cell = row.createCell(1);
            cell.setCellValue(comment.getType());
            cell.setCellStyle(cellStyle);

            cell = row.createCell(2);
            cell.setCellValue(comment.getNickname());
            cell.setCellStyle(cellStyle);

            cell = row.createCell(3);
            cell.setCellValue(comment.getCommentDate());
            cell.setCellStyle(cellStyle);

            cell = row.createCell(4);
            cell.setCellValue(comment.getContent());
            cell.setCellStyle(cellStyle);

            cell = row.createCell(5);
            cell.setCellValue(comment.getAppreciation());
            cell.setCellStyle(cellStyle);
        }

//        String path = Constants.COMMENTS_PATH + StringUtils.dealWithFilename(musicCommentMessage.getSongTitle()) + Constants.COMMENTS_SUFFIX;
        String path = "c:/sgt/" + musicCommentMessage.getSongTitle() + ".xlsx";
        FileOutputStream fos = new FileOutputStream(path);
        workbook.write(fos);
        fos.close();
    }

}
